package com.tro.mail.job;


import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.tro.common.util.email.MailUtil;
import com.tro.common.util.string.AESTool;
import com.tro.mail.dao.entity.MailAnnex;
import com.tro.mail.dao.entity.MailAnnexExample;
import com.tro.mail.dao.entity.MailExternalList;
import com.tro.mail.dao.entity.MailExternalListExample;
import com.tro.mail.dao.entity.MailMain;
import com.tro.mail.dao.entity.MailSetingTable;
import com.tro.mail.dao.entity.MailSetingTableExample;
import com.tro.mail.dao.mapper.MailAnnexMapper;
import com.tro.mail.dao.mapper.MailExternalListMapper;
import com.tro.mail.dao.mapper.MailMainMapper;
import com.tro.mail.dao.mapper.MailSetingTableMapper;
import com.tro.mail.service.MailMainManager;
import com.tro.mail.util.MailStatus;
import com.tro.mail.util.SendEmail;

public class DemoJob {
	
	@Autowired
	MailMainMapper mainmail;
	
	@Autowired
	MailAnnexMapper mailannex;
	
	@Autowired
	MailExternalListMapper mailexter;
	
	@Autowired
	MailSetingTableMapper mailsetingM;
	
	@Autowired
	MailMainManager mailManager;
	
	public void doJob() throws UnsupportedEncodingException, MessagingException{
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		path=path.substring(0,path.length()-17);
		MailUtil mu=new MailUtil();
		MailExternalListExample mailexex=new MailExternalListExample();
		mailexex.createCriteria().andSendStatuEqualTo((short)0).andRetryCountBetween((long)0,(long)5);
		List<MailExternalList> liste=mailexter.selectByExample(mailexex);
		for(MailExternalList e:liste){
			List<Map<String, Object>> listMap=new ArrayList<Map<String,Object>>();
			MailMain mlist = mainmail.selectByPrimaryKey(e.getMailId());
			MailAnnexExample mailannEx=new MailAnnexExample();
			mailannEx.createCriteria().andMailIdEqualTo(e.getMailId());
			List<MailAnnex> lista=mailannex.selectByExample(mailannEx);
			for(MailAnnex a:lista){
				File f=null;
				if(!StringUtils.isBlank(a.getAnnexPath())){
					Map<String, Object> map =new HashMap<String, Object>();
					f=new File(path+a.getAnnexPath());
					map.put("fileName", a.getAnnexName());
					map.put("file", f);
				    listMap.add(map);
				}
			}
			MailSetingTableExample examples=new MailSetingTableExample();
			examples.createCriteria().andDefultEqualTo("1").andSysCodeEqualTo(e.getFromUser());
			List<MailSetingTable> lists = mailsetingM.selectByExample(examples);
			String sendName="";
			String sendPassword="";
			AESTool aes = new AESTool();
			for(MailSetingTable s:lists){
				sendName=s.getEmailCode();
				sendPassword=aes.decrypt(s.getEmailPassword());
			}
			
			List<String> listPerson=new ArrayList<String>();
			listPerson.add(e.getToUser());
			boolean flag=mu.sendSigln(sendName, sendPassword, listMap,listPerson , mlist.getMailContent(), mlist.getMailTitle());
			if(flag){
				e.setSendStatu((short)1);
				mailexter.updateByPrimaryKey(e);
			}else{
				if(e.getRetryCount()>5){//发送邮件提醒发送者消息发送失败
					MailMain receipt = new MailMain();
					receipt.setCreateTime(new Date());
					receipt.setCreateUserId(MailStatus.MAIL_SYS);
					receipt.setMailContent("发送到邮箱："+e.getToUser()+"邮件发送失败");
					receipt.setMailTitle("<系统回执>");
					receipt.setMailStatu(MailStatus.MAIL_SYS_TAG);
					receipt.setConsigneeVal(e.getFromUser());
					receipt.setCreateUserName("系统邮件");
					mailManager.sendMailN(receipt);
				}
				e.setRetryCount(e.getRetryCount()+1);
				mailexter.updateByPrimaryKey(e);
			}
		}
	}
}
